package org.test4j.integration.junit4.helper;


import org.junit.runners.model.FrameworkMethod;
import org.test4j.integration.junit4.DataFrom;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * JUnit框架方法装饰
 */
public class FrameworkParameterMethodHelper {

    /**
     * 将junit测试方法转换为test4j测试方法
     *
     * @param methods
     * @return
     */
    public static List<FrameworkMethod> computeTestParaMethods(Class testClass, List<FrameworkMethod> methods) {
        List<FrameworkMethod> list = new ArrayList<>();
        for (FrameworkMethod method : methods) {
            List<FrameworkMethod> pms = FrameworkParameterMethodHelper.withParameter(testClass, method);
            list.addAll(pms);
        }
        return list;
    }

    private static List<FrameworkMethod> withParameter(Class testClass, FrameworkMethod method) {
        DataFrom dataFrom = method.getMethod().getAnnotation(DataFrom.class);
        if (dataFrom == null) {
            return Arrays.asList(method);
        } else {
            return DataFromHelper.computeParameterizedTestMethods(testClass, method.getMethod(), dataFrom);
        }
    }
}